package org.example.completed.controller;


import org.example.completed.domain.Order;
import org.example.completed.model.vo.ResultVO;
import org.example.completed.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 插入订单信息
     * @param order 订单实体
     * @return 插入成功的记录数
     */
    @PostMapping("/insert")
    public ResultVO<?> insert(@RequestBody Order order) {
        orderService.addOrder(order);
        return ResultVO.ok();
    }

    /**
     * 根据 ID 删除订单信息
     * @param id 订单 ID
     * @return 删除成功的记录数
     */
    @PostMapping("/delete/{id}")
    public ResultVO<?> deleteById(@PathVariable String id) {
        orderService.deleteOrder(id);
        return ResultVO.ok();
    }

    /**
     * 更新订单信息
     * @param order 订单实体
     * @return 更新成功的记录数
     */
    @PostMapping("/update")
    public ResultVO<?> update(@RequestBody Order order) {
        orderService.updateOrder(order);
        return ResultVO.ok();
    }

    /**
     * 根据 ID 查询订单信息
     * @param id 订单 ID
     * @return 订单实体
     */
    @GetMapping("/select/{id}")
    public ResultVO<Order> selectById(@PathVariable String id) {
        Order order = orderService.getOrderById(id);
        return ResultVO.ok(order);
    }

    /**
     * 查询所有订单信息
     * @return 订单列表
     */
    @GetMapping("/selectAll")
    public ResultVO<List<Order>> selectAll() {
        List<Order> orders = orderService.getAllOrders();
        return ResultVO.ok(orders);
    }

    @PostMapping("/buyPet")
    public ResultVO<?> buyPet(@RequestParam String userId, @RequestParam String productId) {
        return orderService.buyPet(userId, productId);
    }
}

